<template>
  <div class="search">
    <!-- 搜索组件一级路由   $router.back()返回上一个页面-->
    <van-nav-bar
      left-arrow
      title="搜索中心"
      fixed
      @click-left="$router.back()">
    </van-nav-bar>

    <!-- https://youzan.github.io/vant/#/zh-CN/search -->
    <van-search
      show-action
      placeholder="请输入搜索关键词"
      shape="round"
      v-model.trim="keyword"
      @input="getSuggestion"
    >
      <template #action>
        <div @click="clickSearch">搜索</div>
      </template>
    </van-search>

    <!-- 联想建议 -->
    <van-cell-group v-if="keyword">
      <van-cell v-for='(item, index) in showSuggestion' :key="item" icon="search" >
        <div v-html="item" @click="clickSuggestion(index)"></div>
      </van-cell>
    </van-cell-group>
    <!-- /联想建议 -->

    <!-- 搜索历史记录 -->
    <van-cell-group v-else>
      <van-cell title="历史记录" v-if="history.length>0">
      </van-cell>
      <van-cell v-for="(item, index) in history" :key="index" :title="item" @click="clickHistory(item)">
        <van-icon name="close" @click.stop="delhistory(index)" />
      </van-cell>
    </van-cell-group>
    <!-- /搜索历史记录 -->
  </div>
</template>

<script>
import { reqGetSuggestion } from '@/api/search.js'
import { setHistory, getHistory } from '@/utils/storage.js'
export default {
  name: 'Search',
  data () {
    return {
      keyword: '', // 搜索关键字
      suggestion: [],
      timeId: null,
      history: getHistory()
    }
  },
  computed: {
    showSuggestion () {
      const regExp = new RegExp(this.keyword, 'gi')
      return this.suggestion.map(item => {
        const newStr = item.replace(regExp, (match) => {
          return `<span style="color: red;">${match}</span>`
        })
        return newStr
      })
    }

  },

  methods: {
    // 获取搜索建议
    getSuggestion () {
      clearTimeout(this.timeId)

      this.timeId = setTimeout(async () => {
        if (this.keyword.trim() === '') {
          this.suggestion = []
          return
        }
        const res = await reqGetSuggestion(this.keyword)
        // console.log(res.data.data)
        this.suggestion = res.data.data.options
      }, 300)
    },
    // 添加搜索历史记录
    addHistory (str) {
      const index = this.history.findIndex(item => item === str)
      if (index !== -1) {
        this.history.splice(index, 1)
      }
      this.history.unshift(str)
      setHistory(this.history)
    },
    // 删除历史记录
    delhistory (index) {
      this.history.splice(index, 1)
      setHistory(this.history)
    },
    // 点击搜索按钮
    clickSearch () {
      if (this.keyword.trim() === '') {
        return
      }
      this.addHistory(this.keyword)
      this.$router.push('/searchResult?keyword=' + this.keyword)
    },
    // 点击搜索建议
    clickSuggestion (index) {
      // console.log(this.suggestion)
      this.addHistory(this.suggestion[index])
      this.$router.push('/searchResult?keyword=' + this.suggestion[index])
    },
    clickHistory (item) {
      this.addHistory(item)// 使历史记录提升
      this.$router.push('/searchResult?keyword=' + item)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
